package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsResourceMapper;
import com.itheima.pojo.PmsProductAttributeCategory;
import com.itheima.pojo.UmsResource;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;


import java.util.List;

@DubboService
public class UmsResourceImpl implements UmsResourceApi{
    @Autowired
    private UmsResourceMapper umsResourceMapper;

    /**
     * 添加
     * @param umsResource
     */
    @Override
    public Boolean save(UmsResource umsResource) {
        //先进行查询 类型名称是否重复
        LambdaQueryWrapper<UmsResource> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsResource::getName,umsResource.getName());
        UmsResource pmsBrand1 = umsResourceMapper.selectOne(wrapper);
        //如果存在 则重复 返回 FALSE
        if(!ObjectUtils.isEmpty(pmsBrand1)){
            return false;
        }
        //走新增
        umsResourceMapper.insert(umsResource);
        return true;

    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        umsResourceMapper.deleteById(id);
    }

    /**
     * 根据id修改
     * @param id
     * @param umsResource
     */
    @Override
    public void update(Integer id, UmsResource umsResource) {
        umsResourceMapper.updateById(umsResource);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public UmsResource findId(Integer id) {
        UmsResource umsResource = umsResourceMapper.selectById(id);
        return umsResource;
    }

    /**
     * 获取资源相关资源列表
     */
    @Override
    public List<UmsResource> findByIds(List<Long> resourceIds) {
        List<UmsResource> umsResources = umsResourceMapper.selectBatchIds(resourceIds);
        return umsResources;
    }

    /**
     * 查找所有
     * @return
     */
    @Override
    public List<UmsResource> findAll() {
        List<UmsResource> umsResources = umsResourceMapper.selectList(new LambdaQueryWrapper<UmsResource>().ne(UmsResource::getId, 0));
        return umsResources;
    }

    /**分页
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @return
     */
    @Override
    public IPage<UmsResource> list(Integer pageNum, Integer pageSize, Integer categoryId, String nameKeyword, String urlKeyword) {
        IPage<UmsResource> pg=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsResource> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(!ObjectUtil.isEmpty(categoryId),UmsResource::getCategoryId,categoryId)
         .like(!ObjectUtil.isEmpty(nameKeyword),UmsResource::getName,nameKeyword)
         .like(!ObjectUtil.isEmpty(urlKeyword),UmsResource::getUrl,urlKeyword);

        return umsResourceMapper.selectPage(pg,wrapper);
    }
}
